package com.example.tys.baseapplication.net_tcp.frame.future;


import com.example.tys.baseapplication.net_tcp.frame.packet.BasePacket;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author tys
 */
public class DefaultFuture<R extends BasePacket> extends AbstractCompletionFuture<R> {

    private volatile ReentrantLock lock = new ReentrantLock();
    private volatile Condition condition = lock.newCondition();

    public DefaultFuture(String msgId) {
        super(msgId);
    }

    @Override
    public void signal() {
        lock.lock();
        try {
            condition.signal();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public void await() throws InterruptedException {
        lock.lock();
        try {
            condition.await();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public void await(long time, TimeUnit unit) throws InterruptedException, TimeoutException {
        lock.lock();
        try {
            condition.await(time, unit);
        }finally {
            lock.unlock();
        }
    }
}
